package com.entrogy.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
 * @author xlr
 * @date 2019-4
 * 注意：实体类 必须要有空参构造
 * 注解@Excel，其他还有@ExcelCollection，@ExcelEntity ，@ExcelIgnore，@ExcelTarget等 了解的官网地址：http://easypoi.mydoc.io/
 */
public class EasyPoiUtil {

	private static final Logger logger = LoggerFactory.getLogger(EasyPoiUtil.class);

	public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){
		ExportParams exportParams = new ExportParams(title, sheetName);
		exportParams.setCreateHeadRows(isCreateHeader);
		defaultExport(list, pojoClass, fileName, response, exportParams);

	}
	public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
		defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
	}
	public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
		defaultExport(list, fileName, response);
	}

	private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
		Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
		if (workbook != null);
		downLoadExcel(fileName, response, workbook);
	}

	private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setHeader("content-Type", "application/vnd.ms-excel");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			logger.error("downLoadExcel"+e.getMessage());
		}
	}
	private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
		Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
		if (workbook != null);
		downLoadExcel(fileName, response, workbook);
	}

	public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
		if (StrUtils.isNullOrEmpty(filePath)){
			return null;
		}
		ImportParams params = new ImportParams();
		params.setTitleRows(titleRows);
		params.setHeadRows(headerRows);
		List<T> list = null;
		try {
			list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
		}catch (NoSuchElementException e){
			logger.error("模板不能为空");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("importExcel"+e.getMessage());
		}
		return list;
	}
	public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
		if (file == null){
			return null;
		}
		ImportParams params = new ImportParams();
		params.setTitleRows(titleRows);
		params.setHeadRows(headerRows);
		List<T> list = null;
		try {
			list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
		}catch (NoSuchElementException e){
			logger.error("excel文件不能为空");
		} catch (Exception e) {
			logger.error("importExcel"+e.getMessage());
		}
		return list;
	}
	//  @RequestMapping("export")   //TODO test 试例
	//    public void export(HttpServletResponse response){
	//
	//        //模拟从数据库获取需要导出的数据
	//        List<Person> personList = new ArrayList<>();
	//        Person person1 = new Person("路飞","1",new Date());
	//        Person person2 = new Person("娜美","2", DateUtils.addDate(new Date(),3));
	//        Person person3 = new Person("索隆","1", DateUtils.addDate(new Date(),10));
	//        Person person4 = new Person("小狸猫","1", DateUtils.addDate(new Date(),-10));
	//        personList.add(person1);
	//        personList.add(person2);
	//        personList.add(person3);
	//        personList.add(person4);
	//
	//        //导出操作
	//        FileUtil.exportExcel(personList,"花名册","草帽一伙",Person.class,"海贼王.xls",response);
	//    }
	//
	//    @RequestMapping("importExcel")
	//    public void importExcel(){
	//        String filePath = "F:\\海贼王.xls";
	//        //解析excel，
	//        List<Person> personList = FileUtil.importExcel(filePath,1,1,Person.class);
	//        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
	//        System.out.println("导入数据一共【"+personList.size()+"】行");
	//
	//        //TODO 保存数据库
	//    }
}
